package co.fitstart.admin.web.controller.courseware;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.resolver.Preloading;

import co.fitstart.admin.service.MovementService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.entity.courseware.movement.Movement;

/**
 * 
 * @author Wy
 *
 */

@Controller
@RequestMapping("/movements/{movementId}")
@MenuSetting("menu-course")
public class MovementEditController {
	
	private static final Logger logger = LoggerFactory.getLogger(MovementAttributeController.class);
	
	@Autowired
    private MovementService movementService;
	
	/**
     *view edit the movements
     * 
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("movementId") Long movementId) {
        
        ModelAndView modelAndView = new ModelAndView("courseware/movement/movement-edit");
        
        Movement movement= movementService.load(Movement.class, movementId);
        
        modelAndView.addObject("movement", movement);
               
        return modelAndView;
    }
    
    
    /**
     * onCreate the movements
     * 
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.PATCH)
    public @ResponseBody String onEdit(@Preloading("movementId") Movement movement
    		                            ) {
        
        Result result = new Result();
        
        try {
        	
        	movementService.update(movement.edit());
        	
            result.success().message("修改成功！");
            
        } catch(Exception e) {
            
            logger.error("#onEdit() - ", e);
            
            result.fail().message("修改失败！");
            
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     *view edit the movements
     * 
     * @return
     */
    @RequestMapping(value = "/cover", method = RequestMethod.GET)
    public ModelAndView editCover(@PathVariable("movementId") Long movementId) {
        
        ModelAndView modelAndView = new ModelAndView("courseware/movement/movement-cover-edit");
        
        Movement movement= movementService.load(Movement.class, movementId);
        
        modelAndView.addObject("movement", movement);
               
        return modelAndView;
    }
    
    
    /**
     * onCreate the movements
     * 
     * @return
     */
    @RequestMapping(value = "/cover", method = RequestMethod.PATCH)
    public @ResponseBody String onEditCover(@PathVariable("movementId") Long movementId,
    		                           @RequestParam("cover") String cover ) {
        
        Result result = new Result();
        
        try {
        	Movement movement = movementService.load(Movement.class, movementId);
        	
        	movementService.update(movement.editCover(cover));
        	
            result.success().message("修改缩略图成功！");
            
        } catch(Exception e) {
            
            logger.error("#onEditCover() - ", e);
            
            result.fail().message("修改缩略图失败！");
            
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     *view edit the movements
     * 
     * @return
     */
    @RequestMapping(value = "/video", method = RequestMethod.GET)
    public ModelAndView editVideo(@PathVariable("movementId") Long movementId) {
        
        ModelAndView modelAndView = new ModelAndView("courseware/movement/movement-video-edit");
        
        Movement movement= movementService.load(Movement.class, movementId);
        
        modelAndView.addObject("movement", movement);
               
        return modelAndView;
    }
    
    
    /**
     * onCreate the movements
     * 
     * @return
     */
    @RequestMapping(value = "/video", method = RequestMethod.PATCH)
    public @ResponseBody String onEditVideo(@PathVariable("movementId") Long movementId,
                                    @RequestParam("video") String video) {
        
        Result result = new Result();
        
        try {
            Movement movement = movementService.load(Movement.class, movementId);
            
            movementService.editVideo(movement, video);
            
            result.success().message("修改视频地址成功！");
            
        } catch(Exception e) {
            
            logger.error("#onEditVideo() - ", e);
            
            result.fail().message("修改视频地址失败！");
            
        }
        
        return JsonUtils.toJsonString(result);
        
    }
    
}
